package comxin.web;

import com.google.gson.Gson;
import comxin.web.bean.User;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class AjaxServlet extends BaseServlet{
    protected void regist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("访问了");
//        Person person = new Person(1,"张三");
        Gson gson=new Gson();
//        String personJsonString = gson.toJson(person);
//        System.out.println(personJsonString);
//        Map<String,Object> map=new HashMap<>();
//        map.put("test1",true);
//        map.put("test2",false);
//        String s = gson.toJson(map);
//        System.out.println(s);
        boolean a=true;
        String s = gson.toJson(a);
        System.out.println(s);
        resp.getWriter().write(s);
    }

   /*
       练习Ajax的用法
    */
    protected void testAjax(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("textAjax方法执行了");
        Gson gson = new Gson();
        User user = new User("admin", "123");
        String userjson = gson.toJson(user);
        System.out.println(userjson);
        PrintWriter writer = resp.getWriter();
        writer.write(userjson);
    }
}
